import React, { Component } from 'react';
import { connect, } from 'react-redux';
import { SET_USER, SET_CODE, SET_GAME_INFO } from './actions/actions'
import request from './api/request'
import { appid, getUrl, getCode, getUserInfo, openSherd, baseURL, setSherd } from './utils/utils'
import Sherd from './components/Sherd'

class App extends Component {

    constructor(props) {
        super(props)
        this.state = {
            isStr: false,
            openid: ''
        }
    }

    componentDidMount() {
        const { dispatch, user } = this.props

        let localStorageUser = localStorage.getItem('user')
        if (localStorageUser != 'undefined' && localStorageUser != null) {
            console.log(localStorageUser)
            localStorageUser = JSON.parse(localStorageUser)
            this.setState({ openid: localStorageUser.openid })
            getUserInfo(dispatch, localStorageUser.openid)
            setSherd(localStorageUser.openid, dispatch)
            return
        }
        const code = getUrl().code;
        const inviter = getUrl().inviter

        if (code) {
            dispatch({
                type: SET_CODE,
                data: code
            })
            request.getWXAccessToken({
                code,
                inviter,
                source: 1
            }).then(res => {
                dispatch({
                    type: SET_USER,
                    data: res.data || null
                })
                this.setState({ openid: res.data.openid })
                setSherd(res.data.openid, dispatch)
            })
        } else {
            getCode()
        }
    }


    render() {
        const { user, gameInfo } = this.props
        const { isStr } = this.state
        console.log(this, 'props')
        console.log(this.props, 'props')
        console.log(user && user.openid, '用户信息')
        console.log(user.openid, '用户信息2')
        let userItem = localStorage.getItem('user')
        let str = JSON.stringify(this.props.user)
        if (user.openid) {
            return (
                <div className="container" >
                    <div className="debugging">
                        <p onClick={() => localStorage.clear()}>清除缓存</p>
                        <p onClick={() => this.setState({ isStr: !isStr })}>{isStr ? '关闭调试' : '打开调试'}</p>
                    </div>
                    {isStr ? (
                        <div className="debugging-box">
                            <div className="debugging-info">
                                <p>{userItem}</p>
                                <p>{str}</p>
                                <p>{JSON.stringify(window.location)}</p>
                            </div>
                        </div>
                    ) : null}
                    {this.props.children}
                    <Sherd />
                </div>
            );
        } else {
            return (
                <div className="container">
                    {/* <div className="debugging">
                        <p onClick={() => localStorage.removeItem('user')}>清除缓存</p>
                        <p onClick={() => this.setState({ isStr: !isStr })}>{isStr ? '关闭调试' : '打开调试'}</p>
                    </div> */}
                    {isStr ? (
                        <div className="debugging-box">
                            <div className="debugging-info">
                                <p>{userItem}</p>
                                <p>{str}</p>
                                <p>{JSON.stringify(window.location)}</p>
                            </div>
                        </div>
                    ) : null}

                </div>
            )
        }
    }
}


export default connect(({ user, code, gameInfo }) => ({ user, code, gameInfo }))(App);
